//
//  ViewController.m
//  MTDBFROMJSON
//
//  Created by kingPC on 13-8-22.
//  Copyright (c) 2013年 kingPC. All rights reserved.
//

#import "ViewController.h"
#import "MTDBOperation.h"
#import "AppDelegate.h"
#import "JSONKit.h"
#import "Question.h"
#import "GameView.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize audioPlayer;
- (void)viewDidLoad
{
    [super viewDidLoad];
    Qarray =[[NSMutableArray alloc]init];
    
    NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"Music_13"
                                                          ofType:@"mp3"];
    if (musicPath) {
        NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL
                                                             error:nil];
        [audioPlayer setDelegate:self];
    }
    
    if ([audioPlayer isPlaying]) {
        [audioPlayer stop];
    }
    else {
        [audioPlayer play];
    }
    
//    NSString *context=[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://aunm123.jhost.cn/new/q.php"] encoding:NSUTF8StringEncoding error:nil];
//    
//    
//    NSDictionary *d=[context objectFromJSONString];
//    NSArray *a=[d objectForKey:@"list"];
//    
//    MTDBOperation *dOperation=[[MTDBOperation alloc]init];
//    
//    for (NSDictionary *td in a) {
//        NSMutableDictionary *md=[[[NSMutableDictionary alloc]initWithDictionary:td]autorelease];
//        [dOperation UpDataDB:md createTableSQL:CreateTestTableSQL tableName:@"t_test"];
//    }
//    [dOperation release];
//    
//    DatabaseOperation *db=[[DatabaseOperation alloc]initWithDbName:database_name];
//    NSArray *resource=[db querryTable:@"select * from `t_test`"];
//    
//    NSLog(@"%@",resource);
    
    
	// Do any additional setup after loading the view, typically from a nib.
//    @try {
//        NSString *context=[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://aunm123.jhost.cn/new/question2.php"] encoding:NSUTF8StringEncoding error:nil];
//        
//        if (context==Nil||[context isKindOfClass:[NSNull class]]) {
//            NSLog(@"error to connect");
//            conent=NO;
//        }else{
//            conent=YES;
//            NSDictionary *d=[context objectFromJSONString];
//            NSArray *a=[d objectForKey:@"list"];
//        
//            MTDBOperation *dOperation=[[MTDBOperation alloc]init];
//        
//            for (NSDictionary *td in a) {
//                NSMutableDictionary *md=[[[NSMutableDictionary alloc]initWithDictionary:td]autorelease];
//                NSArray *answersA=[td objectForKey:@"answerList"];
//                for (NSDictionary *a in answersA) {
//                    NSMutableDictionary *b=[[[NSMutableDictionary alloc]initWithDictionary:a]autorelease];
//                    [b setValue:[md objectForKey:@"id"] forKey:@"Qid"];
//                    [dOperation UpDataDB:b createTableSQL:Answers_TableSQL  tableName:@"t_answers"];
//                }
//                [md removeObjectForKey:@"answerList"];
//                [dOperation UpDataDB:md createTableSQL:Question_TableSQL tableName:@"t_question"];
//            }
//            [dOperation release];
//        }
//    }
//    @catch (NSException *exception) {
//        NSLog(@"%@",[exception description]);
//    }
    
    
    
//    DatabaseOperation *db2=[[[DatabaseOperation alloc]initWithDbName:database_name]autorelease];
//    NSString *s=[NSString stringWithFormat:@"delete from `%@`",@"t_question"];
//    [db2 UpdataTable:s];
//    NSString *s2=[NSString stringWithFormat:@"delete from `%@`",@"t_answers"];
//    [db2 UpdataTable:s2];
    
    CGSize size=[UIScreen mainScreen].bounds.size;
    
    self.GoldView.frame=CGRectMake(size.width-148, 0, 148, size.height);
    self.GoldView.RootVC=self;
    [self.view addSubview:self.GoldView];
    [self.view sendSubviewToBack:self.GoldView];

}

-(void)dealloc{
    [_gameView release];
    [audioPlayer release];
    [_GoldView release];
    [super dealloc];
}


-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
                       successfully:(BOOL)flag{
    [audioPlayer play];
}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player{
    [audioPlayer play];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)GmaeViewLoad{
    if ([audioPlayer isPlaying]) {
        [audioPlayer stop];
    }
    
    if (self.gameView) {
        [self.gameView removeFromSuperview];
        [Qarray removeAllObjects];
        if ([self.gameView.audioPlayer isPlaying]) {
            [self.gameView.audioPlayer stop];
        }
    }
    
    [self.view bringSubviewToFront:self.GoldView];
    
    DatabaseOperation *db=[[[DatabaseOperation alloc]initWithDbName:database_name]autorelease];
    NSArray *resource=[db querryTable:@"select * from `t_question` order by RANDOM() limit 1"];
    
    
    for (NSDictionary *d in resource) {
        Question *q=[[Question alloc]initWithDirectory:d];
        [Qarray addObject:q];
        [q release];
    }
    
    self.gameView=[[GameView alloc]initWithQuestion:[Qarray objectAtIndex:0
                                                     ]];
    CGSize size=[UIScreen mainScreen].bounds.size;
    self.gameView.frame=CGRectMake(0, 0, size.width, size.height);
    [self.gameView backGroundMusicRUN];
    self.gameView.viewcontroller=self;
    [self.gameView viewLoadinit];
    [self.view addSubview:self.gameView];
}

- (IBAction)Start:(id)sender {
//    if (conent) {
        [self GmaeViewLoad];
//    }
}

-(void)NextView{
    CGSize size=[UIScreen mainScreen].bounds.size;
    if (self.gameView) {
        [UIView animateWithDuration:0.5 animations:^(void){
            self.gameView.frame=CGRectMake(-148, 0, size.width, size.height);
        }completion:^(BOOL finish){
            [self.GoldView nextView];
        }];
    }
}

-(void)restartNextView{
    CGSize size=[UIScreen mainScreen].bounds.size;
    if (self.gameView) {
        if (self.gameView.frame.origin.x!=0) {
            [UIView animateWithDuration:0.5 animations:^(void){
                self.gameView.frame=CGRectMake(0, 0, size.width, size.height);
            }completion:^(BOOL finish){
                [self GmaeViewLoad];
            }];
        }
    }
}

- (void)viewDidUnload {
    [self setGoldView:nil];
    [super viewDidUnload];
}
@end
